package com.wl156.activities.order;

import java.io.IOException;

import org.xmlpull.v1.XmlPullParserException;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;

import com.wl156.activities.find.FindResultBase;
import com.wl156.adapters.OrderAdapter;
import com.wl156.lite.R;

public class DriverMyOrderResult extends FindResultBase {

	String endLocationLv1;
	String endLocationLv2;
	String endLocationLv3;
	String freightMessageId;
	String CargosDescription;
	String companyName;
	String contactTel;
	String tonnage;
	String truckTonnageSymBol;
	String acceptDateStart;
	String acceptDateEnd;
	String[] orderStatus;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		mHeaderStub.setLayoutResource(R.layout.item_order_header);
		mHeaderStub.inflate();

		Bundle extras = getIntent().getExtras();

		hideBottomButtons();

		startLocationLv1 = extras.getString("startLocationLv1");
		startLocationLv2 = extras.getString("startLocationLv2");
		startLocationLv3 = extras.getString("startLocationLv3");
		endLocationLv1 = extras.getString("endLocationLv1");
		endLocationLv2 = extras.getString("endLocationLv2");
		endLocationLv3 = extras.getString("endLocationLv3");
		freightMessageId = extras.getString("freightMessageId");
		CargosDescription = extras.getString("CargosDescription");
		companyName = extras.getString("LogisticCompanyName");
		contactTel = extras.getString("contactTel");
		tonnage = extras.getString("tonnage");
		truckTonnageSymBol = extras.getString("truckTonnageSymBol");
		acceptDateStart = extras.getString("acceptDateStart");
		acceptDateEnd = extras.getString("acceptDateEnd");
		orderStatus = new String[] { extras.getString("orderStatus") };

		new FindTask(type).execute();
	}

	@Override
	protected int getPhoneIndex() {
		return 11;
	}

	@Override
	protected String getFindName() {
		return "我的订单";
	}

	@Override
	protected Class getFindActivity() {
		return MyOrder.class;
	}

	@Override
	protected Class getFindResultActivity() {
		return DriverMyOrderResult.class;
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);

		Intent i = new Intent(DriverMyOrderResult.this, MyOrderDetail.class);
		i.putExtra("id", String.valueOf(id));
		Log.d(TAG, "id: " + id);
		startActivity(i);
	}

	@Override
	protected void onLastItemVisible() {
		super.onLastItemVisible();

		new FindTask(type).execute();
	}

	public class FindTask extends FindTaskBase {

		public FindTask(int type) {
			super(type);
		}

		@Override
		protected void onLoadFailed() {
			super.onLoadFailed();

			new FindTask(type).execute();
		}

		@Override
		protected Object doInBackground(Void... params) {
			try {
				// 08-02 22:12:08.621: D/(617): responseDump: <?xml
				// version="1.0" encoding="utf-8"?><soap:Envelope
				// xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
				// xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
				// xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><driver_getMyOrdersResponse
				// xmlns="http://202.104.188.11:8080/"><driver_getMyOrdersResult>
				// <ArrayOfString><string>182</string><string>8537655390-10626194855</string><string>广东珠海</string><string>北京北京</string><string>1</string><string>300</string><string>2012-06-26</string></ArrayOfString><ArrayOfString><string>181</string><string>2903404230-10626171620</string><string>广东珠海</string><string>北京北京</string><string>1</string><string>560</string><string>2012-06-26</string></ArrayOfString></driver_getMyOrdersResult></driver_getMyOrdersResponse></soap:Body></soap:Envelope>

				return mWebServiceClient.driver_getMyOrders(mCertificate,
						startLocationLv1, startLocationLv2, startLocationLv3,
						endLocationLv1, endLocationLv2, endLocationLv3,
						CargosDescription, companyName, contactTel, tonnage,
						truckTonnageSymBol, acceptDateStart, acceptDateEnd,
						mCurrentPageIndex, countPerPage, orderStatus);
			} catch (IOException e) {
				e.printStackTrace();
				return e;
			} catch (XmlPullParserException e) {
				e.printStackTrace();
				return e;
			}
		}
	}

	@Override
	protected Class getAdapter() {
		return OrderAdapter.class;
	}

}
